package memoryinst.preprocessors
import scala.io.Source
import java.io._

class CoreProcessor(input:String,output:String) {

  var lines_output:scala.collection.mutable.ListBuffer[String]=new scala.collection.mutable.ListBuffer[String];

  val topStart="ReaderStatusProc::statusTopStart();\n"
  val topEnd  ="ReaderStatusProc::statusTopEnd();\n"
  
  val printStartTop="ReaderStatusProc::printTopStart();\n"
  val printEndTop  ="ReaderStatusProc::printTopEnd();\n"  
  
    
  val start_region="ReaderStatusProc::startRegion(\"%s\",\"%s\",\"%s\",%s);\n"
  

  val reduce      ="ReaderStatusProc::reduce();\n"//reduction des deux informations à l'aide d'un operateur
  
    
    
  private val writer = new PrintWriter(new File(output))
      
  private def closeInstrumentation()={
	  writer write topEnd
	  writer write printEndTop
      writer write reduce  
  }
  
  
  
  def processInput()={
    println("Processing the input file!")
    
    var annotations:scala.collection.mutable.Stack[String]=new scala.collection.mutable.Stack[String]
    
    try{
       val lines=Source.fromFile(input).getLines
       
       //chaque ligne commançant par //@instrument doit être parsée
       
       lines.foreach(line => {
    	   						if(line.contains("//@end")){
    	   						  //ici generer le code d'instrumentation topEnd()
    	   						  //depiler le sommet, generer le bloc de code corespondant
    	   						  //val sommet = annotations.pop();
    	   						  closeInstrumentation()  				     
    	   						}
    	   						
    	   						if ((line.length>12) && (line.substring(0,13)=="//@instrument")){
       							  val tokens =  line.split(" ",3)
       							  //tokens.foreach(println)
       							  
       							  //ici matcher le type d'instrumentation
       							  tokens(1) match{
       							    case "status"   => //@instrument status {function:f file:statusInstrumenter.cpp line:5}  
       							    				   if(tokens.length==3){
       							    				     val inst_args = tokens(2).replaceAll("\\{","").replaceAll("\\}","").replaceAll("\n","").split(" ")
       							    				     
       							    				     val function  = inst_args(0)
       							    				     val file      = inst_args(1)
       							    				     val line      = inst_args(2)
       							    				     
       							    				     writer write String.format(start_region,"status",function.split(":")(1),file.split(":")(1),line.split(":")(1)) 
       							    				     writer write topStart
       							    				     writer write printStartTop
       							    				     
       							    				     
       							    				   }
       							    case "mallinfo" => if(tokens.length==3){
       							    				     val inst_args = tokens(2).replaceAll("\\{","").replaceAll("\\}","").replaceAll("\n","").split(" ")
       							    				     
       							    				     val function  = inst_args(0)
       							    				     val file      = inst_args(1)
       							    				     val line      = inst_args(2)
       							    				     
       							    				     writer write String.format(start_region,"mallinfo",function.split(":")(1),file.split(":")(1),line.split(":")(1)) 
       							    				     writer write topStart
       							    				     writer write printStartTop
       							    				     
       							    				   }
       							    case _          =>
       							  }
       							}
    	   						
    	   						
    	   						
       							//lines_output+=line;
       							writer write line	
       							})
       
      }catch{
        case ex:java.io.FileNotFoundException => println ("Verfiez que le fichier en entrée !")
      }
  }
  
  def generateOuput()={
    println("Closing the output file!")
    writer close
  }
}